<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('选择代理商')"/>
</head>
<body class="gray-bg">
<div class="col-sm-12 search-collapse">
    <form id="formId">
        <div class="select-list">
            <ul>
                <li>
                    代理商编号：
                    <input type="text" name="agentId" id="agentId"/>
                </li>
                <li>
                    <label>渠道选择：</label>
                    <select name="channelCode" class="form-control m-b"
                            onchange="$.table.search()"
                            th:with="type=${@dict.getType('channel_type')}">
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                    </select>
                </li>
                <li>
                    <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()">
                        <i class="fa fa-search"></i>&nbsp;搜索
                    </a>
                    <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()">
                        <i class="fa fa-refresh"></i>&nbsp;重置
                    </a>
                </li>
            </ul>
        </div>
    </form>
</div>


<div class="container-div">
    <div class="row">
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>

<div th:include="include :: footer"></div>

<script th:inline="javascript">
    $(function () {
        var options = {
            url: ctx + "channel/agent/queryAgentInfoList",
            clickToSelect: true,
            columns: [
                {radio: true},
                {
                    field: 'channelCode',
                    title: '渠道编码'
                },
                {
                    field: 'agentId',
                    title: '代理商编号'
                },
                {
                    field: 'cagentId',
                    title: '渠道代理商编号'
                },
                {
                    field: 'enterpriseTitle',
                    title: '公司名称'
                },
                {
                    field: 'legalPersonName',
                    title: '法人姓名'
                }
            ]
        };
        $.table.init(options);
    });

    // 提交选择
    function submitHandler() {
        var agentId = $.table.selectColumns("agentId")[0] || '';
        var cagentId = $.table.selectColumns("cagentId")[0] || '';
        var enterpriseTitle = $.table.selectColumns("enterpriseTitle")[0] || '';
        var legalPersonName = $.table.selectColumns("legalPersonName")[0] || '';
        var channelCode = $.table.selectColumns("channelCode")[0] || '';
        var agentName = enterpriseTitle.trim() !== '' ? enterpriseTitle.trim() : legalPersonName;

        if (agentId.length === 0) {
            $.modal.alertWarning("请至少选择一条记录");
            return;
        }
        //  根据 obj 传值给不同字段（如有多个用途）
        parent.$("#agentId").val(agentId);
        parent.$("#applyAgentId").val(cagentId);
        parent.$("#agentName").val(agentName);
        parent.$("#channelCode").val(channelCode);
        // 手动触发 input 和 blur 事件，触发父页面的监听逻辑
        parent.$("#agentName").trigger("input");
        parent.queryWithdraw();
        parent.toggleBalanceFields(channelCode);
        $.modal.close();
    }
</script>
</body>
</html>
